
/*
This file is part of MJIN2.

Copyright (C) 2012-2013 Opensource Game Studio

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#ifndef MJIN2_SOUND_SOURCE_MULTI_BUFFER_H
#define MJIN2_SOUND_SOURCE_MULTI_BUFFER_H

#include <mjin2/SoundBuffer.h>
#include <mjin2/SoundSourceBuffer.h>
#include <mjin2/SoundSystem.h>

#include <list>

namespace mjin2
{

//! Source that can play several buffers at once the FIFO way.
class SoundSourceMultiBuffer
{
    public:
        //! \param buffersNb Number of buffers
        SoundSourceMultiBuffer(u8 buffersNb) throw ();
        ~SoundSourceMultiBuffer() throw ();

        SOUND_SOURCE_STATE state() const throw ();
        float volume() const throw ();
        bool isEnabled() const throw ();
        //! This pushes Buffer along the FIFO pipe and plays it.
        //! Also, removes Buffers that stopped playing.
        //! \param skipSame Skip the buffer if we have it already.
        void pushBuffer(SoundBuffer *buf, bool skipSame = false) throw (Exception);
        void setEnabled(bool enabled) throw (Exception);
        //! Removes all Buffers if AL_STOPPED passed.
        void setState(SOUND_SOURCE_STATE state) throw (Exception);
        void setVolume(float value) throw (Exception);

    private:
        const u8 mBuffersNb;
        float mVolume;
        bool mEnabled;

        void removeAll() throw ();
        void removeFirstPair() throw ();
        void removeStoppedPairs() throw ();

        struct Pair
        {
            Pair(SoundBuffer *buf) throw (Exception);
            ~Pair() throw (Exception);

            SoundSourceBuffer *source;
            SoundBuffer *buffer;
        };
        typedef std::list<Pair *> Pairs;
        Pairs mPairs;
};

} // namespace mjin

#endif // MJIN2_SOUND_SOURCE_MULTI_BUFFER_H

